package com.cn.mmd.controller;

import com.cn.mmd.model.*;
import com.cn.mmd.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 对他人进行评价的控制类（各个身份的人对老师的评价）
 * Created by Administrator on 2016/7/13.
 */

@Controller
@RequestMapping(value = "ToEvaluateOthers")
public class ToEvaluateOthersController {

    @Autowired
    EvaluateTeacherService evaluateTeacherService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    StudentService studentService;
    @Autowired
    LogController logController;
    @Autowired
    CourseService courseService;
    @Autowired
    TokenController tokenController;
    @Autowired
    StudentAppService studentAppService;
    @Autowired
    MajorCourseNumController majorCourseNumController;
    @Autowired
    StuCourseLabelService stuCourseLabelService;

    /**
     * 查出所有同教学小组的老师
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "SelectSameGroup")
    public synchronized
    @ResponseBody
    Map<String, Object> SelectSameGroup(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();

        String teacherId = request.getParameter("teacherId");

        String token=request.getParameter("token");
//        System.out.println("token:"+token);

        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(teacherId, token);
        if (teacherData==null){
            map.put("code", 204);
            map.put("message", "您暂无权限做此操作");
          //  map.put("data", "");
            return map;
        }

       // System.out.println(teacherId);
        List<TeachingGroup> TeachingGroup = evaluateTeacherService.selectTeachingGroupId(teacherId);//查出老师所在教学小组的id


        if (TeachingGroup.size() > 0) {
            List<TeacherData> list=new ArrayList<TeacherData>();
            for (int i = 0; i < TeachingGroup.size(); i++) {
                //System.out.println("查出来的小组id："+TeachingGroup.get(i).getTeachinggroupid());

                List<TeachingGroup> teachingGroups = evaluateTeacherService.selectTeachingGroup(TeachingGroup.get(i).getTeachinggroupid());//查出教学组中所有老师id

                //System.out.println(teachingGroups.size());
                for (int j = 0; j < teachingGroups.size(); j++) {
                    //System.out.println("查出来的老师id："+teachingGroups.get(j).getTeacherid());
                    TeacherData teachers = teacherService.selectByTeacherId(teachingGroups.get(j).getTeacherid());
                    if (teachers!=null){

                      //  System.out.println("查出来的老师id："+teachers.getTeacherid());

                        if (!teacherId.equals(teachers.getTeacherid())){
                            list.add(teachers);
                        }
                    }else {
                        //System.out.println("他为空");
                        map.put("code", 202);
                        map.put("message", "未查出有同小组的老师");
//                        map.put("data", "");
                        return map;
                    }
                }
            }

//            List<TeacherData> list1=new ArrayList<TeacherData>();

            List<Map<String, Object>> mapList=new ArrayList<Map<String, Object>>();
            for (int a=0;a<list.size();a++){
                Map<String, Object> map1 = new HashMap<String, Object>();
                for (int b=a+1;b<list.size();b++){
                    if (list.get(a).getTeacherid().equals(list.get(b).getTeacherid())){
                        list.remove(b);//去除重复查出的教师
                    }
                }
//                TeacherData teacherData=new TeacherData();
//                teacherData.setTeacherid(list.get(a).getTeacherid());
//                teacherData.setTeachername(list.get(a).getTeachername());
//                teacherData.setTeacherheadportrait(list.get(a).getTeacherheadportrait());
//                teacherData.setMynickname(list.get(a).getMynickname());
//                list1.add(teacherData);

                map1.put("teacherId",list.get(a).getTeacherid());//教师id
                map1.put("teacherName",list.get(a).getTeachername());//教师姓名
                if (list.get(a).getTeacherheadportrait()!=null){
                    map1.put("teacherHeadPortrait",list.get(a).getTeacherheadportrait());
                }else {
                    map1.put("teacherHeadPortrait","");
                }
                mapList.add(map1);

            }
            map.put("code", 200);
            map.put("message", "查询成功");
            map.put("data", mapList);

        } else {
            map.put("code", 203);
            map.put("message", "您未加入任何小组！");
//            map.put("data", "");
        }

        return map;
    }


    /**
     * 学生查找自己的任课老师
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "SelectMyTeacher")
    public synchronized
    @ResponseBody
    Map<String, Object> SelectMyTeacher(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = null;
        List<Map<String, Object>> mapList=new ArrayList<Map<String, Object>>();

        String studentId=request.getParameter("studentId");//学生id
        String token = request.getParameter("token");

        StudentApp studentApp;
        studentApp = (StudentApp) tokenController.token1(studentId, token);
        if (studentApp==null){
            map.put("code", 204);
            map.put("message", "您暂无权限做此操作");
//            map.put("data", "");
            return map;
        }

        //System.out.println(studentId);
        Student student=studentService.selectByKey(studentId);//查找学生班级
        if (student!=null){
            String classes=student.getStudentclass();
            //System.out.println(classes);
            List<Course> courses=courseService.selectTeacherByClass(classes);
            //System.out.println(courses.size());
            if (courses.size()>0){
                for (int i=0;i<courses.size();i++){
                    map1 = new HashMap<String, Object>();
                    String teacherIds=courses.get(i).getCourseteacherid();//教师工号
                    String courseName=courses.get(i).getCoursename();//课程名
                    String teacherId="";
                    String teacherHeadPortrait="";
                    String teacherName="";
                    //System.out.println(teacherIds);
                    if (teacherIds==null){
                        continue;
                    }
                    String[] teacherid=teacherIds.split("/");
                    TeacherData teacherData=null;
                    int num=0;
                    for (int a=0;a<teacherid.length;a++){
                        teacherId=teacherid[a];
                        teacherData=teacherService.selectByTeacherId(teacherId);
                        if (teacherData==null){
                            map.put("code", 203);
                            map.put("message", "教师信息查询失败");
//                            map.put("data", "");
                        }
                        if (teacherData.getTeacherheadportrait()!=null){
                            teacherHeadPortrait=teacherData.getTeacherheadportrait();//头像
                        }

                        if (teacherHeadPortrait==null){
                            teacherHeadPortrait="";
                        }
                        teacherName=teacherData.getTeachername();//教师名
                        num=evaluateTeacherService.countCustomEvaluation(teacherId);

                    }
                    map1.put("teacherId",teacherId);
                    map1.put("teacherName",teacherName);
                    map1.put("teacherHeadPortrait",teacherHeadPortrait);
                    map1.put("courseName",courseName);
                    map1.put("evaluateNum",num);

                    mapList.add(map1);
                }
                map.put("code", 200);
                map.put("message", "查询成功");
                map.put("data", mapList);

            }else {
                //这个班没人教
                map.put("code", 202);
                map.put("message", "未查到授课教师");
//                map.put("data", "");
            }

        }else {
            //该学生没查到
            map.put("code", 201);
            map.put("message", "未查出您的信息");
//            map.put("data", "");
        }

    return map;
    }



    /**
     * 对同事进行标签评价（督导评价或同事评价）
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "evaluationColleagues")
    public synchronized
    @ResponseBody
    Map<String, Object> evaluationColleagues(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
       // Map<String, Object> map1 = new HashMap<String, Object>();

        String teacherId = request.getParameter("colleagueId");//前端传入评价人id
        String byEvaluatePersonId = request.getParameter("byEvaluatePersonId");//前端传入被评价人id
        String evaluateLabel = request.getParameter("evaluateLabel");//前端传入所选标签内容
        String token=request.getParameter("token");

        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(teacherId, token);
        if (teacherData==null){
            map.put("code", 212);
            map.put("message", "您暂无权限做此操作");
          //  map.put("data", "");
            return map;
        }

        if (teacherId.equals(byEvaluatePersonId)){
            map.put("code", 201);
            map.put("message", "您不能给自己做评价");
//            map.put("data", "");
            return map;
        }
        Teacher teachers = teacherService.serlectTeacher(byEvaluatePersonId);//查出该老师属于哪个学校和学院

        List<TeachingGroup> TeachingGroup = evaluateTeacherService.selectTeachingGroupId(byEvaluatePersonId);//查出老师所在教学小组的id
        if (TeachingGroup.size()>0){
            //System.out.println("到这里！！1");
            for (int i=0;i<TeachingGroup.size();i++) {
               // System.out.println("到这里！！2");
                List<TeachingGroup> teachingGroups = evaluateTeacherService.selectTeachingGroup(TeachingGroup.get(i).getTeachinggroupid());
                //System.out.println("到这里！！3");
                for (int x = 0; x < teachingGroups.size(); x++) {
                   // System.out.println("到这里！！4");
                    if (
//                            teachingGroups.get(x).getTeacherid().equals(byEvaluatePersonId) &&
                            teacherId.equals(TeachingGroup.get(i).getTeachinggroupmanageid())) {
                        System.out.println("进入督导评价");
                        //评价人是管理者（督导评价）
                        String[] evaluate = evaluateLabel.split(",");//label:{"标签库名！三级指标！标签"，"标签库名！三级指标！标签"...}

                        List<Label> labelList = new ArrayList<Label>();
                        for (int a = 0; a < evaluate.length; a++) {
                            String[] label = evaluate[a].split("!");

                            String packageName=label[0];
                            String threeIndexName = label[1];
                            String labelName = label[2];

                            //System.out.println(indexName + "======" + labelName);

                            Label label2=new Label();
                            label2.setPackagelabel(packageName);
                            label2.setLabelname(labelName);
                        //    label2.setIndexname(indexName);

//                            System.out.println("标签："+labelName);
                            Label label1 = evaluateTeacherService.selectAllLabel(label2);//查出一个标签属于哪个指标，分值多少
                            if (label1 != null) {
                                labelList.add(label1);
                            } else {
                                map.put("code", 202);
                                map.put("message", "查询标签失败");
//                                map.put("data", "");
                                return map;
                            }
                        }

                        String indexScore = "";

                        List<LabelThreeIndex> labelThreeIndexes = new ArrayList<LabelThreeIndex>();
                        // Float avg=0f;
                        for (int l = 0; l < labelList.size(); l++) {
                            List<Float> score = new ArrayList<Float>();
                            for (int m = labelList.size() - 1; m > l; m--) {
                                if (labelList.get(l).getIndexname().equals(labelList.get(m).getIndexname()) &&
                                        labelList.get(l).getTwoindexname().equals(labelList.get(m).getTwoindexname()) &&
                                        labelList.get(l).getThreeindexname().equals(labelList.get(m).getThreeindexname())) {

                                    score.add(labelList.get(m).getLabelgrade());
                                    labelList.remove(m);

                                }
                            }
                            score.add(labelList.get(l).getLabelgrade());
                            Float sum = 0f;
                            for (Float s : score) {
                                sum += s;
                            }
                           // System.out.println("和：" + sum + "====标签数：" + score.size());
                            Float avg = sum / score.size();
                            //Float avg1 =   (float)(Math.round(avg*10))/10;

                            LabelThreeIndex threeIndex=new LabelThreeIndex();
                            threeIndex.setThreeindexname(labelList.get(l).getThreeindexname());
                            threeIndex.setPackagelabel(labelList.get(l).getPackagelabel());

                            LabelThreeIndex labelThreeIndex = evaluateTeacherService.selectByThreeIndexName(threeIndex);
                            if (labelThreeIndex != null) {
                                //System.out.println(labelThreeIndex.getThreeindexname() + "===" + labelThreeIndex.getProportion());
                                Float threeIndexScore = avg * labelThreeIndex.getProportion();//乘以权重  得出该三级指标得分

                                LabelThreeIndex labelThreeIndex1 = new LabelThreeIndex();
                                labelThreeIndex1.setThreeindexname(labelList.get(l).getThreeindexname());
                                labelThreeIndex1.setTwoindexname(labelList.get(l).getTwoindexname());
                                labelThreeIndex1.setIndexname(labelList.get(l).getIndexname());
                                labelThreeIndex1.setPackagelabel(labelList.get(l).getPackagelabel());
                                labelThreeIndex1.setThreeIndexScore(threeIndexScore);
                               // System.out.println("三级指标：" + labelList.get(l).getThreeindexname() + "---得分：" + threeIndexScore);
                                labelThreeIndexes.add(labelThreeIndex1);//存入一个list labelThreeIndexes中，里面全部为不同三级指标的得分
                            } else {
                                map.put("code", 203);
                                map.put("message", "查询三级指标失败");
//                                map.put("data", "");
                                return map;
                            }
                        }
                        // //循环比较二级指标下的标签,算出第二级指标的得分

                        List<LabelTwoIndex> labelTwoIndexes = new ArrayList<LabelTwoIndex>();
                        for (int l = 0; l < labelThreeIndexes.size(); l++) {
                            List<Float> score = new ArrayList<Float>();
                            for (int m = labelThreeIndexes.size() - 1; m > l; m--) {
                                if (labelThreeIndexes.get(l).getIndexname().equals(labelThreeIndexes.get(m).getIndexname()) &&
                                        labelThreeIndexes.get(l).getTwoindexname().equals(labelThreeIndexes.get(m).getTwoindexname())) {
                                    score.add(labelThreeIndexes.get(m).getThreeIndexScore());
                                    labelThreeIndexes.remove(m);
                                }
                            }
                            score.add(labelThreeIndexes.get(l).getThreeIndexScore());
                            Float sum = 0f;
                            for (Float s : score) {
                                sum += s;
                            }
                            //System.out.println(labelThreeIndexes.get(l).getTwoindexname() + "-的和：" + sum);

                            LabelTwoIndex twoIndex=new LabelTwoIndex();
                            twoIndex.setTwoindexname(labelThreeIndexes.get(l).getTwoindexname());
                            twoIndex.setPackagelabel(labelThreeIndexes.get(l).getPackagelabel());

//                            System.out.println("二级指标："+labelThreeIndexes.get(l).getTwoindexname()+"  "+labelThreeIndexes.get(l).getPackagelabel());
                            LabelTwoIndex labelTwoIndex = evaluateTeacherService.selectByTwoIndexName(twoIndex);
                            if (labelTwoIndex != null) {
                                Float twoIndexScore = sum * labelTwoIndex.getProportion();
                                LabelTwoIndex labelTwoIndex1 = new LabelTwoIndex();
                                labelTwoIndex1.setIndexname(labelTwoIndex.getIndexname());
                                labelTwoIndex1.setTwoindexname(labelTwoIndex.getTwoindexname());
                                labelTwoIndex1.setPackagelabel(labelTwoIndex.getPackagelabel());
                                labelTwoIndex1.setTwoindexscore(twoIndexScore);

                                labelTwoIndexes.add(labelTwoIndex1);//存入一个list labelTwoIndexes，里面全部为不同二级指标的得分
                            } else {
                                map.put("code", 204);
                                map.put("message", "查询二级指标失败");
//                                map.put("data", "");
                                return map;
                            }

                            if (indexScore .equals("") ) {
                                indexScore =labelThreeIndexes.get(l).getPackagelabel() + "!" + labelThreeIndexes.get(l).getTwoindexname() + "!" + sum;
                            } else {
                                indexScore = indexScore + "," + labelThreeIndexes.get(l).getPackagelabel() + "!" + labelThreeIndexes.get(l).getTwoindexname() + "!" + sum;
                            }

                        }


                        //计算一级指标得分

                        List<LabelIndex> labelIndexes = new ArrayList<LabelIndex>();
                        for (int l = 0; l < labelTwoIndexes.size(); l++) {
                            List<Float> score = new ArrayList<Float>();
                            for (int m = labelTwoIndexes.size() - 1; m > l; m--) {
                                if (labelTwoIndexes.get(l).getIndexname().equals(labelTwoIndexes.get(m).getIndexname())) {
                                    score.add(labelTwoIndexes.get(m).getTwoindexscore());
                                    labelTwoIndexes.remove(m);

                                }
                            }
                            score.add(labelTwoIndexes.get(l).getTwoindexscore());
                            Float sum = 0f;
                            Float sum1 = 0f;
                            for (Float s : score) {
                                sum += s;
                                sum1 = (float) (Math.round(sum * 100)) / 100;
                            }
                           // System.out.println(labelTwoIndexes.get(l).getIndexname() + "-的和：" + sum1);//还未乘以权重的值

                            LabelIndex index=new LabelIndex();
                            index.setIndexname(labelTwoIndexes.get(l).getIndexname());
                            index.setPackagelabel(labelTwoIndexes.get(l).getPackagelabel());

                            LabelIndex labelIndex = evaluateTeacherService.selectIndex(index);

                            if (labelIndex != null) {
                                Float IndexScore = sum * labelIndex.getProportion();
                                LabelIndex labelIndex1 = new LabelIndex();  //乘以权重后的值
                                labelIndex1.setIndexname(labelIndex.getIndexname());
                                labelIndex1.setProportion(labelIndex.getProportion());
                                labelIndex1.setPackagelabel(labelIndex.getPackagelabel());
                                //labelIndex1.setIndexname(labelIndex.getIndexname());
                                labelIndex1.setIndexscore(IndexScore);

                                labelIndexes.add(labelIndex1);//存入一个list labelIndexes，里面全部为不同一级指标的得分
                            } else {
                                map.put("code", 205);
                                map.put("message", "查询一级指标失败");
//                                map.put("data", "");
                                return map;
                            }



                            // map1.put(labelTwoIndexes.get(l).getIndexname(), sum1);
                            //System.out.println("指标得分" + indexScore);
                        }

                        //算出这次评价的得分
                        Float sum = 0f;
                        for (LabelIndex s : labelIndexes) {
                            sum += s.getIndexscore();
                            sum = (float) (Math.round(sum * 100)) / 100;//这次评价的得分
                        }
                        Date now = new Date();
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
                        String evaluateTime = dateFormat.format(now);//时间

//                        Teacher teachers = teacherService.serlectTeacher(byEvaluatePersonId);//查出该老师属于哪个学校和学院
                        //添加到同行评价表中
                        SupervisorEvaluate supervisorEvaluate = new SupervisorEvaluate
                                (teacherId, byEvaluatePersonId, sum, evaluateLabel, evaluateTime, indexScore,teachers.getTeachercollege());
                        int b = evaluateTeacherService.insertSupervisorEvaluation(supervisorEvaluate);
                        if (b > 0) {

                            //添加日志！！
                            //操作内容


                            String logContent="督导评价“"+byEvaluatePersonId+"”成功";
                            //System.out.println(logContent);
                            logController.logTeacher(logContent, teacherId);



                            map.put("code", 200);
                            map.put("message", "评价成功");
//                            map.put("data", "");
                        } else {
                            map.put("code", 400);
                            map.put("message", "评价失败");
//                            map.put("data", "");
                        }
                        //将此次评价分数整合或添加到被评价人的的排名表中

                        TeacherRanking teacherRanking = teacherService.selectTeacherRanking(byEvaluatePersonId);
                        String supervisorTwoIndexScore="";

                        if (teacherRanking != null) {
                            List<String> list=new ArrayList<String>();
                            TeacherRanking teacherRanking1 = new TeacherRanking();
                            teacherRanking1.setTeacherid(byEvaluatePersonId);

                            String [] indexScores=indexScore.split(",");
                            for (int c=0;c<indexScores.length;c++) {
                                String twoIndexName = indexScores[c].split("!")[1];//这次评价的二级指标名
                                float twoScore = Float.parseFloat(indexScores[c].split("!")[2]);//二级指标得分

                                if (teacherRanking.getSupervisortwoindexscore()!=null){

                                    String [] supervisorTwoIndexScores=teacherRanking.getSupervisortwoindexscore().split("!!");//排名表中已有的督导二级指标

                                    for (int a=0;a<supervisorTwoIndexScores.length;a++){

                                        String[] Twoindexscore = supervisorTwoIndexScores[a].split("_");
                                        String twoIndex = Twoindexscore[0];//已有的二级指标名
                                        float score = Float.parseFloat(Twoindexscore[1]);//总分数
                                        int number = Integer.parseInt(Twoindexscore[2]);//总人数

                                        list.add(twoIndex);

                                        if (twoIndex.equals(twoIndexName)) {
                                            score = score + twoScore;
                                            number = number + 1;

                                            if (supervisorTwoIndexScore.equals("")) {
                                                supervisorTwoIndexScore = twoIndex + "_" + score + "_" + number;
                                            } else {
                                                supervisorTwoIndexScore = supervisorTwoIndexScore + "!!" + twoIndex + "_" + score + "_" + number;
                                            }
                                        }
//                                        else {
//                                            if (supervisorTwoIndexScore.equals("")) {
//                                                supervisorTwoIndexScore = twoIndex + "_" + score + "_" + number;
//                                            } else {
//                                                supervisorTwoIndexScore = supervisorTwoIndexScore + "!!" + twoIndex + "_" + score + "_" + number;
//                                            }
//                                        }
                                    }

                                }else {
                                    if (supervisorTwoIndexScore.equals("")) {
                                        supervisorTwoIndexScore = twoIndexName + "_" + twoScore + "_" + 1;
                                    } else {
                                        supervisorTwoIndexScore = supervisorTwoIndexScore + "!!" + twoIndexName + "_" + twoScore + "_" + 1;
                                    }
                                }
                                if (!list.contains(twoIndexName)){
                                    if (supervisorTwoIndexScore.equals("")) {
                                        supervisorTwoIndexScore = twoIndexName + "_" + twoScore + "_" + 1;
                                    } else {
                                        supervisorTwoIndexScore = supervisorTwoIndexScore + "!!" + twoIndexName + "_" + twoScore + "_" + 1;
                                    }
                                }
                            }

                            teacherRanking1.setSupervisortwoindexscore(supervisorTwoIndexScore);

//                            System.out.println("到底有多长？"+supervisorTwoIndexScore+"  "+supervisorTwoIndexScore.length());
                            int a = teacherService.updateTeacherRanking(teacherRanking1);
                            if (a <= 0) {
                                map.put("code", 206);
                                map.put("message", "整合教师排名失败");
//                                map.put("data", "");
                                return map;
                            }
                        } else {

                            TeacherRanking teacherRanking1 = new TeacherRanking();
                            teacherRanking1.setTeacherid(byEvaluatePersonId);

                            String [] indexScores=indexScore.split(",");
                            for (int c=0;c<indexScores.length;c++) {
                                String twoIndexName = indexScores[c].split("!")[1];//这次评价的二级指标名
                                float twoScore = Float.parseFloat(indexScores[c].split("!")[2]);//二级指标得分

                                if (supervisorTwoIndexScore.equals("")) {
                                    supervisorTwoIndexScore = twoIndexName + "_" + twoScore + "_" + 1;
                                } else {
                                    supervisorTwoIndexScore = supervisorTwoIndexScore + "!!" + twoIndexName + "_" + twoScore + "_" + 1;
                                }
                            }

                            teacherRanking1.setSupervisortwoindexscore(supervisorTwoIndexScore);

                            int a = teacherService.insertTeacherRanking(teacherRanking1);

                            if (a <= 0) {
                                map.put("code", 206);
                                map.put("message", "整合教师排名失败");
//                                map.put("data", "");
                                return map;
                            }
                        }
                        //还要将此次评价分数整合到avg_school表和avg_college表中！！！！！！！！！！！！
//                        Teacher teachers =teacherService.serlectTeacher(byEvaluatePersonId);//查出该老师属于哪个学校和学院
                        //将此次评价分数整合到avg_school表
                        AvgSchool avgSchool = teacherService.selectAvgSchool(); // 这里！！！！！！！！！！！！！！！！！！！！！！1
                        System.out.println("查到没有？"+avgSchool);
                        if (avgSchool != null) {
                            if (teachers != null) {

                                String supervisorTwoIndexScore1="";

                                String[] indexScores=indexScore.split(",");
                                for (int c=0;c<indexScores.length;c++) {
                                    String twoIndexName = indexScores[c].split("!")[1];//这次评价的二级指标名
                                    float twoScore = Float.parseFloat(indexScores[c].split("!")[2]);//二级指标得分
                                    if (avgSchool.getSuptwoindexscore() != null) {

                                        String[] supervisorTwoIndexScores = avgSchool.getSuptwoindexscore().split("!!");//学校表中已经有的二级指标；双感叹号分割
//                                        String[] studentTwoindexscores = avgSchool.getStutwoindexscore().split("!!");//学生评价已有的二级指标；双感叹号分割

                                        List<String> list=new ArrayList<String>();
//                                        List<String> list1=new ArrayList<String>();

                                        //学校督导二级指标评价统计
                                        for (int a = 0; a < supervisorTwoIndexScores.length; a++) {
                                            String[] Twoindexscore = supervisorTwoIndexScores[a].split("_");
                                            String twoIndex = Twoindexscore[0];//已有的二级指标名
                                            float score = Float.parseFloat(Twoindexscore[1]);//总分数
                                            int number = Integer.parseInt(Twoindexscore[2]);//总人数

                                            list.add(twoIndex);

                                            if (twoIndex.equals(twoIndexName)) {
                                                score = score + twoScore;
                                                number = number + 1;

                                                if (supervisorTwoIndexScore1.equals("")) {
                                                    supervisorTwoIndexScore1 = twoIndex + "_" + score + "_" + number;
                                                } else {
                                                    supervisorTwoIndexScore1 = supervisorTwoIndexScore1 + "!!" + twoIndex + "_" + score + "_" + number;
                                                }
                                            }
//                                            else {
//                                                if (supervisorTwoIndexScore1.equals("")) {
//                                                    supervisorTwoIndexScore1 = twoIndex + "_" + score + "_" + number;
//                                                } else {
//                                                    supervisorTwoIndexScore1 = supervisorTwoIndexScore1 + "!!" + twoIndex + "_" + score + "_" + number;
//                                                }
//                                            }
                                        }
                                        if (!list.contains(twoIndexName)){
                                            if (supervisorTwoIndexScore1.equals("")) {
                                                supervisorTwoIndexScore1 = twoIndexName + "_" + twoScore + "_" + 1;
                                            } else {
                                                supervisorTwoIndexScore1 = supervisorTwoIndexScore1 + "!!" + twoIndexName + "_" + twoScore + "_" + 1;
                                            }
                                        }

                                    } else {
                                        supervisorTwoIndexScore1 = twoIndexName + "_" + twoScore + "_" + 1;
                                    }
                                }

                                AvgSchool avgSchool1 = new AvgSchool();
                                avgSchool1.setSchoolid(teachers.getTeacherschool());
                                avgSchool1.setSuptwoindexscore(supervisorTwoIndexScore1);

//                                System.out.println("修改后："+supervisorTwoIndexScore1);

                                int a = teacherService.updateAvgSchool(avgSchool1);
                                if (a <= 0)  {
                                    map.put("code", 207);
                                    map.put("message", "修改学校平均分失败");
//                                    map.put("data", "");
                                    return map;
                                }
                            } else {
                                map.put("code", 208);
                                map.put("message", "学校查询失败");
//                                map.put("data", "");
                                return map;
                            }
                        } else {

                            if (teachers != null) {

                                String[] indexScores=indexScore.split(",");
                                String twoIndexScoreN="";
                                for (int e=0;e<indexScores.length;e++){

                                    String twoIndexName = indexScores[e].split("!")[1];//二级指标名
                                    float twoScore = Float.parseFloat(indexScores[e].split("!")[2]);//二级指标得分

                                    if (twoIndexScoreN.equals("")){
                                        twoIndexScoreN=twoIndexName+"_"+twoScore+"_"+1;
                                    }else {
                                        twoIndexScoreN=twoIndexScoreN+"!!"+twoIndexName+"_"+twoScore+"_"+1;
                                    }
                                }

                                AvgSchool avgSchool1 = new AvgSchool();
                                avgSchool1.setSchoolid(teachers.getTeacherschool());
                                avgSchool1.setSuptwoindexscore(twoIndexScoreN);

                                int a = teacherService.insertAvgSchool(avgSchool1);
                                if (a > 0) {
                                    //System.out.println("成功");
                                } else {
                                    map.put("code", 207);
                                    map.put("message", "添加学校平均分失败");
//                                    map.put("data", "");
                                    return map;
                                }

                            } else {
                                map.put("code", 208);
                                map.put("message", "学校查询失败");
//                                map.put("data", "");
                                return map;
                            }
                        }
                        //将此次评价分数整合到avg_college表

                        if (teachers != null) {
                            AvgCollege avgCollege = teacherService.selectAvgCollege(teachers.getTeachercollege());
                            AvgCollege avgCollege1 = new AvgCollege();
                            if (avgCollege != null) {

                                String supervisorTwoIndexScore1="";
//                                String studentTwoindexscore1="";
                                String[] indexScores=indexScore.split(",");
                                for (int c=0;c<indexScores.length;c++) {
                                    String twoIndexName = indexScores[c].split("!")[1];//这次评价的二级指标名
                                    float twoScore = Float.parseFloat(indexScores[c].split("!")[2]);//二级指标得分
                                    if (avgCollege.getSuptwoindexscore() != null) {

                                        String[] Twoindexscores = avgCollege.getSuptwoindexscore().split("!!");//学校表中已经有的二级指标；双感叹号分割
//                                        String[] studentTwoindexscores = avgCollege.getStutwoindexscore().split("!!");//学生评价已有的二级指标；双感叹号分割

                                        List<String> list=new ArrayList<String>();
//                                        List<String> list1=new ArrayList<String>();

                                        //学院督导二级指标评价统计
                                        for (int a = 0; a < Twoindexscores.length; a++) {
                                            String[] Twoindexscore = Twoindexscores[a].split("_");
                                            String twoIndex = Twoindexscore[0];//已有的二级指标名
                                            float score = Float.parseFloat(Twoindexscore[1]);//总分数
                                            int number = Integer.parseInt(Twoindexscore[2]);//总人数

                                            list.add(twoIndex);

                                            if (twoIndex.equals(twoIndexName)) {
                                                score = score + twoScore;
                                                number = number + 1;

                                                if (supervisorTwoIndexScore1.equals("")) {
                                                    supervisorTwoIndexScore1 = twoIndex + "_" + score + "_" + number;
                                                } else {
                                                    supervisorTwoIndexScore1 = supervisorTwoIndexScore1 + "!!" + twoIndex + "_" + score + "_" + number;
                                                }
                                            }
//                                            else {
//                                                if (supervisorTwoIndexScore1.equals("")) {
//                                                    supervisorTwoIndexScore1 = twoIndex + "_" + score + "_" + number;
//                                                } else {
//                                                    supervisorTwoIndexScore1 = supervisorTwoIndexScore1 + "!!" + twoIndex + "_" + score + "_" + number;
//                                                }
//                                            }
                                        }
                                        if (!list.contains(twoIndexName)){
                                            if (supervisorTwoIndexScore1.equals("")) {
                                                supervisorTwoIndexScore1 = twoIndexName + "_" + twoScore + "_" + 1;
                                            } else {
                                                supervisorTwoIndexScore1 = supervisorTwoIndexScore1 + "!!" + twoIndexName + "_" + twoScore + "_" + 1;
                                            }
                                        }
                                    } else {
                                        supervisorTwoIndexScore1 = twoIndexName + "_" + twoScore + "_" + 1;
                                    }
                                }

                                avgCollege1.setSchoolcollegeid(avgCollege.getSchoolcollegeid());
                                avgCollege1.setSuptwoindexscore(supervisorTwoIndexScore1);

                                int a = teacherService.updateAvgCollege(avgCollege1);
                                if (a <= 0) {
                                    map.put("code", 209);
                                    map.put("message", "修改学院平均分失败");
//                                    map.put("data", "");
                                    return map;
                                }
                            } else {
                                String[] indexScores=indexScore.split(",");
                                String twoIndexScoreN="";
                                for (int e=0;e<indexScores.length;e++){

                                    String twoIndexName = indexScores[e].split("!")[1];//二级指标名
                                    float twoScore = Float.parseFloat(indexScores[e].split("!")[2]);//二级指标得分

                                    if (twoIndexScoreN.equals("")){
                                        twoIndexScoreN=twoIndexName+"_"+twoScore+"_"+1;
                                    }else {
                                        twoIndexScoreN=twoIndexScoreN+"!!"+twoIndexName+"_"+twoScore+"_"+1;
                                    }
                                }

                                avgCollege1.setSchoolcollegeid(teachers.getTeachercollege());
                                avgCollege1.setSuptwoindexscore(twoIndexScoreN);

                                int a = teacherService.insertAvgCollege(avgCollege1);
                                if (a <= 0) {
                                    map.put("code", 209);
                                    map.put("message", "添加学院平均分失败");
//                                    map.put("data", "");
                                    return map;
                                }
                            }

                        } else {
                            map.put("code", 210);
                            map.put("message", "学院查询失败");
//                            map.put("data", "");
                            return map;
                        }

                    }
                    else if (
//                            teachingGroups.get(x).getTeacherid().equals(byEvaluatePersonId) &&
                            !teacherId.equals(TeachingGroup.get(i).getTeachinggroupmanageid())){

                        System.out.println("进入同事评价");
//                        //评价人不是管理者（同事评价）


                        String[] evaluate = evaluateLabel.split(",");//label:{"标签库名！三级指标！标签"，"标签库名！三级指标！标签"...}
                        List<Label> labelList = new ArrayList<Label>();

                        for (int a = 0; a < evaluate.length; a++) {
                           String[] label = evaluate[a].split("!");

                           String packageName=label[0];
                           String threeIndexName = label[1];
                           String labelName = label[2];

                           //System.out.println(indexName + "======" + labelName);

                           Label label2=new Label();
                           label2.setPackagelabel(packageName);
                           label2.setLabelname(labelName);
                           //    label2.setIndexname(indexName);

                           Label label1 = evaluateTeacherService.selectAllLabel(label2);//查出一个标签属于哪个指标，分值多少

                            if (label1 != null) {
                               labelList.add(label1);
                            } else {
                               map.put("code", 202);
                               map.put("message", "查询标签失败");
//                               map.put("data", "");
                               return map;
                            }

                        }

                        String indexScore = "";

                        List<LabelThreeIndex> labelThreeIndexes = new ArrayList<LabelThreeIndex>();
                        // Float avg=0f;
                        for (int l = 0; l < labelList.size(); l++) {
                            List<Float> score = new ArrayList<Float>();
                            for (int m = labelList.size() - 1; m > l; m--) {
                                if (labelList.get(l).getIndexname().equals(labelList.get(m).getIndexname()) &&
                                        labelList.get(l).getTwoindexname().equals(labelList.get(m).getTwoindexname()) &&
                                        labelList.get(l).getThreeindexname().equals(labelList.get(m).getThreeindexname())) {

                                    score.add(labelList.get(m).getLabelgrade());
                                    labelList.remove(m);

                                }
                            }
                            score.add(labelList.get(l).getLabelgrade());
                            Float sum = 0f;
                            for (Float s : score) {
                                sum += s;
                            }
                            // System.out.println("和：" + sum + "====标签数：" + score.size());
                            Float avg = sum / score.size();
                            //Float avg1 =   (float)(Math.round(avg*10))/10;

                            LabelThreeIndex threeIndex=new LabelThreeIndex();
                            threeIndex.setThreeindexname(labelList.get(l).getThreeindexname());
                            threeIndex.setPackagelabel(labelList.get(l).getPackagelabel());

                            LabelThreeIndex labelThreeIndex = evaluateTeacherService.selectByThreeIndexName(threeIndex);
                            if (labelThreeIndex != null) {
                                //System.out.println(labelThreeIndex.getThreeindexname() + "===" + labelThreeIndex.getProportion());
                                Float threeIndexScore = avg * labelThreeIndex.getProportion();//乘以权重  得出该三级指标得分

                                LabelThreeIndex labelThreeIndex1 = new LabelThreeIndex();
                                labelThreeIndex1.setThreeindexname(labelList.get(l).getThreeindexname());
                                labelThreeIndex1.setTwoindexname(labelList.get(l).getTwoindexname());
                                labelThreeIndex1.setIndexname(labelList.get(l).getIndexname());
                                labelThreeIndex1.setPackagelabel(labelList.get(l).getPackagelabel());
                                labelThreeIndex1.setThreeIndexScore(threeIndexScore);
                                // System.out.println("三级指标：" + labelList.get(l).getThreeindexname() + "---得分：" + threeIndexScore);
                                labelThreeIndexes.add(labelThreeIndex1);//存入一个list labelThreeIndexes中，里面全部为不同三级指标的得分
                            } else {
                                map.put("code", 203);
                                map.put("message", "查询三级指标失败");
//                                map.put("data", "");
                                return map;
                            }
                        }
                        // //循环比较二级指标下的标签,算出第二级指标的得分

                        List<LabelTwoIndex> labelTwoIndexes = new ArrayList<LabelTwoIndex>();
                        for (int l = 0; l < labelThreeIndexes.size(); l++) {
                            List<Float> score = new ArrayList<Float>();
                            for (int m = labelThreeIndexes.size() - 1; m > l; m--) {
                                if (labelThreeIndexes.get(l).getIndexname().equals(labelThreeIndexes.get(m).getIndexname()) &&
                                        labelThreeIndexes.get(l).getTwoindexname().equals(labelThreeIndexes.get(m).getTwoindexname())) {
                                    score.add(labelThreeIndexes.get(m).getThreeIndexScore());
                                    labelThreeIndexes.remove(m);
                                }
                            }
                            score.add(labelThreeIndexes.get(l).getThreeIndexScore());
                            Float sum = 0f;
                            for (Float s : score) {
                                sum += s;
                            }
                            //System.out.println(labelThreeIndexes.get(l).getTwoindexname() + "-的和：" + sum);

                            LabelTwoIndex twoIndex=new LabelTwoIndex();
                            twoIndex.setTwoindexname(labelThreeIndexes.get(l).getTwoindexname());
                            twoIndex.setPackagelabel(labelThreeIndexes.get(l).getPackagelabel());


                            LabelTwoIndex labelTwoIndex = evaluateTeacherService.selectByTwoIndexName(twoIndex);
                            if (labelTwoIndex != null) {
                                Float twoIndexScore = sum * labelTwoIndex.getProportion();
                                LabelTwoIndex labelTwoIndex1 = new LabelTwoIndex();
                                labelTwoIndex1.setIndexname(labelTwoIndex.getIndexname());
                                labelTwoIndex1.setTwoindexname(labelTwoIndex.getTwoindexname());
                                labelTwoIndex1.setPackagelabel(labelTwoIndex.getPackagelabel());
                                labelTwoIndex1.setTwoindexscore(twoIndexScore);

                                labelTwoIndexes.add(labelTwoIndex1);//存入一个list labelTwoIndexes，里面全部为不同二级指标的得分
                            } else {
                                map.put("code", 204);
                                map.put("message", "查询二级指标失败");
//                                map.put("data", "");
                                return map;
                            }

                            if (indexScore .equals("") ) {
                                indexScore =labelThreeIndexes.get(l).getPackagelabel() + "!" + labelThreeIndexes.get(l).getTwoindexname() + "!" + sum;
                            } else {
                                indexScore = indexScore + "," + labelThreeIndexes.get(l).getPackagelabel() + "!" + labelThreeIndexes.get(l).getTwoindexname() + "!" + sum;
                            }

                        }


                        //计算一级指标得分

                        List<LabelIndex> labelIndexes = new ArrayList<LabelIndex>();
                        for (int l = 0; l < labelTwoIndexes.size(); l++) {
                            List<Float> score = new ArrayList<Float>();
                            for (int m = labelTwoIndexes.size() - 1; m > l; m--) {
                                if (labelTwoIndexes.get(l).getIndexname().equals(labelTwoIndexes.get(m).getIndexname())) {
                                    score.add(labelTwoIndexes.get(m).getTwoindexscore());
                                    labelTwoIndexes.remove(m);

                                }
                            }
                            score.add(labelTwoIndexes.get(l).getTwoindexscore());
                            Float sum = 0f;
                            Float sum1 = 0f;
                            for (Float s : score) {
                                sum += s;
                                sum1 = (float) (Math.round(sum * 100)) / 100;
                            }
                            // System.out.println(labelTwoIndexes.get(l).getIndexname() + "-的和：" + sum1);//还未乘以权重的值

                            LabelIndex index=new LabelIndex();
                            index.setIndexname(labelTwoIndexes.get(l).getIndexname());
                            index.setPackagelabel(labelTwoIndexes.get(l).getPackagelabel());

                            LabelIndex labelIndex = evaluateTeacherService.selectIndex(index);

                            if (labelIndex != null) {
                                Float IndexScore = sum * labelIndex.getProportion();
                                LabelIndex labelIndex1 = new LabelIndex();  //乘以权重后的值
                                labelIndex1.setIndexname(labelIndex.getIndexname());
                                labelIndex1.setProportion(labelIndex.getProportion());
                                labelIndex1.setPackagelabel(labelIndex.getPackagelabel());
                                //labelIndex1.setIndexname(labelIndex.getIndexname());
                                labelIndex1.setIndexscore(IndexScore);

                                labelIndexes.add(labelIndex1);//存入一个list labelIndexes，里面全部为不同一级指标的得分
                            } else {
                                map.put("code", 205);
                                map.put("message", "查询一级指标失败");
//                                map.put("data", "");
                                return map;
                            }



                            // map1.put(labelTwoIndexes.get(l).getIndexname(), sum1);
                            //System.out.println("指标得分" + indexScore);
                        }

                        //算出这次评价的得分
                        Float sum = 0f;
                        for (LabelIndex s : labelIndexes) {
                            sum += s.getIndexscore();
                            sum = (float) (Math.round(sum * 100)) / 100;//这次评价的得分
                        }
                        Date now = new Date();
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
                        String evaluateTime = dateFormat.format(now);//时间

//                        Teacher teachers = teacherService.serlectTeacher(byEvaluatePersonId);//查出该老师属于哪个学校和学院
                        //添加到同行评价表中
                                ColleagueEvaluate colleagueEvaluate = new ColleagueEvaluate
                                (teacherId, byEvaluatePersonId, sum, evaluateLabel, evaluateTime, indexScore,teachers.getTeachercollege());
                        int b = evaluateTeacherService.insertColleagueEvaluation(colleagueEvaluate);
                        if (b > 0) {

                            //添加日志！！
                            //操作内容


                            String logContent="同事评价“"+byEvaluatePersonId+"”成功";
                            //System.out.println(logContent);
                            logController.logTeacher(logContent, teacherId);



                            map.put("code", 200);
                            map.put("message", "评价成功");
//                            map.put("data", "");
                        } else {
                            map.put("code", 400);
                            map.put("message", "评价失败");
//                            map.put("data", "");
                        }
                        //将此次评价分数整合或添加到被评价人的的排名表中

                        TeacherRanking teacherRanking = teacherService.selectTeacherRanking(byEvaluatePersonId);
                        String colleagueTwoIndexScore="";

                        if (teacherRanking != null) {
                            List<String> list=new ArrayList<String>();
                            TeacherRanking teacherRanking1 = new TeacherRanking();
                            teacherRanking1.setTeacherid(byEvaluatePersonId);

                            String [] indexScores=indexScore.split(",");
                            for (int c=0;c<indexScores.length;c++) {
                                String twoIndexName = indexScores[c].split("!")[1];//这次评价的二级指标名
                                float twoScore = Float.parseFloat(indexScores[c].split("!")[2]);//二级指标得分

                                if (teacherRanking.getColleaguetwoindexscore()!=null){

                                    String [] colleagueTwoIndexScores=teacherRanking.getColleaguetwoindexscore().split("!!");//排名表中已有的督导二级指标

                                    for (int a=0;a<colleagueTwoIndexScores.length;a++){

                                        String[] Twoindexscore = colleagueTwoIndexScores[a].split("_");
                                        String twoIndex = Twoindexscore[0];//已有的二级指标名
                                        float score = Float.parseFloat(Twoindexscore[1]);//总分数
                                        int number = Integer.parseInt(Twoindexscore[2]);//总人数

                                        list.add(twoIndex);

                                        if (twoIndex.equals(twoIndexName)) {
                                            score = score + twoScore;
                                            number = number + 1;

                                            if (colleagueTwoIndexScore.equals("")) {
                                                colleagueTwoIndexScore = twoIndex + "_" + score + "_" + number;
                                            } else {
                                                colleagueTwoIndexScore = colleagueTwoIndexScore + "!!" + twoIndex + "_" + score + "_" + number;
                                            }
                                        }
//                                        else {
//                                            if (colleagueTwoIndexScore.equals("")) {
//                                                colleagueTwoIndexScore = twoIndex + "_" + score + "_" + number;
//                                            } else {
//                                                colleagueTwoIndexScore = colleagueTwoIndexScore + "!!" + twoIndex + "_" + score + "_" + number;
//                                            }
//                                        }
                                    }
                                    if (!list.contains(twoIndexName)){
                                        if (colleagueTwoIndexScore.equals("")) {
                                            colleagueTwoIndexScore = twoIndexName + "_" + twoScore + "_" + 1;
                                        } else {
                                            colleagueTwoIndexScore = colleagueTwoIndexScore + "!!" + twoIndexName + "_" + twoScore + "_" + 1;
                                        }
                                    }
                                }else {
                                    if (colleagueTwoIndexScore.equals("")) {
                                        colleagueTwoIndexScore = twoIndexName + "_" + twoScore + "_" + 1;
                                    } else {
                                        colleagueTwoIndexScore = colleagueTwoIndexScore + "!!" + twoIndexName + "_" + twoScore + "_" + 1;
                                    }
                                }
                            }

                            teacherRanking1.setColleaguetwoindexscore(colleagueTwoIndexScore);

                            int a = teacherService.updateTeacherRanking(teacherRanking1);

                            if (a <= 0) {
                                map.put("code", 206);
                                map.put("message", "整合教师排名失败");
//                                map.put("data", "");
                                return map;
                            }
                        } else {

                            TeacherRanking teacherRanking1 = new TeacherRanking();
                            teacherRanking1.setTeacherid(byEvaluatePersonId);

                            String [] indexScores=indexScore.split(",");
                            for (int c=0;c<indexScores.length;c++) {
                                String twoIndexName = indexScores[c].split("!")[1];//这次评价的二级指标名
                                float twoScore = Float.parseFloat(indexScores[c].split("!")[2]);//二级指标得分

                                if (colleagueTwoIndexScore.equals("")) {
                                    colleagueTwoIndexScore = twoIndexName + "_" + twoScore + "_" + 1;
                                } else {
                                    colleagueTwoIndexScore = colleagueTwoIndexScore + "!!" + twoIndexName + "_" + twoScore + "_" + 1;
                                }
                            }

                            teacherRanking1.setColleaguetwoindexscore(colleagueTwoIndexScore);

                            int a = teacherService.insertTeacherRanking(teacherRanking1);

                            if (a <= 0) {
                                map.put("code", 206);
                                map.put("message", "整合教师排名失败");
//                                map.put("data", "");
                                return map;
                            }
                        }
                        //还要将此次评价分数整合到avg_school表和avg_college表中！！！！！！！！！！！！
//                        Teacher teachers =teacherService.serlectTeacher(byEvaluatePersonId);//查出该老师属于哪个学校和学院
                        //将此次评价分数整合到avg_school表
                        AvgSchool avgSchool = teacherService.selectAvgSchool(); // 这里！！！！！！！！！！！！！！！！！！！！！！1
                        if (avgSchool != null) {
                            if (teachers != null) {

                                String colleagueTwoIndexScore1="";

                                String[] indexScores=indexScore.split(",");
                                for (int c=0;c<indexScores.length;c++) {
                                    String twoIndexName = indexScores[c].split("!")[1];//这次评价的二级指标名
                                    float twoScore = Float.parseFloat(indexScores[c].split("!")[2]);//二级指标得分
                                    if (avgSchool.getColltwoindexscore() != null) {

                                        String[] colleagueTwoIndexScores = avgSchool.getColltwoindexscore().split("!!");//学校表中已经有的二级指标；双感叹号分割
//                                        String[] studentTwoindexscores = avgSchool.getStutwoindexscore().split("!!");//学生评价已有的二级指标；双感叹号分割

                                        List<String> list=new ArrayList<String>();
//                                        List<String> list1=new ArrayList<String>();

                                        //学校督导二级指标评价统计
                                        for (int a = 0; a < colleagueTwoIndexScores.length; a++) {
                                            String[] Twoindexscore = colleagueTwoIndexScores[a].split("_");
                                            String twoIndex = Twoindexscore[0];//已有的二级指标名
                                            float score = Float.parseFloat(Twoindexscore[1]);//总分数
                                            int number = Integer.parseInt(Twoindexscore[2]);//总人数

                                            list.add(twoIndex);

                                            if (twoIndex.equals(twoIndexName)) {
                                                score = score + twoScore;
                                                number = number + 1;

                                                if (colleagueTwoIndexScore1.equals("")) {
                                                    colleagueTwoIndexScore1 = twoIndex + "_" + score + "_" + number;
                                                } else {
                                                    colleagueTwoIndexScore1 = colleagueTwoIndexScore1 + "!!" + twoIndex + "_" + score + "_" + number;
                                                }
                                            }
//                                            else {
//                                                if (colleagueTwoIndexScore1.equals("")) {
//                                                    colleagueTwoIndexScore1 = twoIndex + "_" + score + "_" + number;
//                                                } else {
//                                                    colleagueTwoIndexScore1 = colleagueTwoIndexScore1 + "!!" + twoIndex + "_" + score + "_" + number;
//                                                }
//                                            }
                                        }
                                        if (!list.contains(twoIndexName)){
                                            if (colleagueTwoIndexScore1.equals("")) {
                                                colleagueTwoIndexScore1 = twoIndexName + "_" + twoScore + "_" + 1;
                                            } else {
                                                colleagueTwoIndexScore1 = colleagueTwoIndexScore1 + "!!" + twoIndexName + "_" + twoScore + "_" + 1;
                                            }
                                        }

                                    } else {
                                        colleagueTwoIndexScore1 = twoIndexName + "_" + twoScore + "_" + 1;
                                    }
                                }

                                AvgSchool avgSchool1 = new AvgSchool();
                                avgSchool1.setSchoolid(teachers.getTeacherschool());
                                avgSchool1.setColltwoindexscore(colleagueTwoIndexScore);

                                int a = teacherService.updateAvgSchool(avgSchool1);
                                if (a <= 0)  {
                                    map.put("code", 207);
                                    map.put("message", "修改学校平均分失败");
//                                    map.put("data", "");
                                    return map;
                                }
                            } else {
                                map.put("code", 208);
                                map.put("message", "学校查询失败");
//                                map.put("data", "");
                                return map;
                            }
                        } else {

                            if (teachers != null) {

                                String[] indexScores=indexScore.split(",");
                                String twoIndexScoreN="";
                                for (int e=0;e<indexScores.length;e++){

                                    String twoIndexName = indexScores[e].split("!")[1];//二级指标名
                                    float twoScore = Float.parseFloat(indexScores[e].split("!")[2]);//二级指标得分

                                    if (twoIndexScoreN.equals("")){
                                        twoIndexScoreN=twoIndexName+"_"+twoScore+"_"+1;
                                    }else {
                                        twoIndexScoreN=twoIndexScoreN+"!!"+twoIndexName+"_"+twoScore+"_"+1;
                                    }
                                }

                                AvgSchool avgSchool1 = new AvgSchool();
                                avgSchool1.setSchoolid(teachers.getTeacherschool());
                                avgSchool1.setColltwoindexscore(twoIndexScoreN);

                                int a = teacherService.insertAvgSchool(avgSchool1);
                                if (a > 0) {
                                    //System.out.println("成功");
                                } else {
                                    map.put("code", 207);
                                    map.put("message", "添加学校平均分失败");
//                                    map.put("data", "");
                                    return map;
                                }

                            } else {
                                map.put("code", 208);
                                map.put("message", "学校查询失败");
//                                map.put("data", "");
                                return map;
                            }
                        }
                        //将此次评价分数整合到avg_college表

                        if (teachers != null) {
                            AvgCollege avgCollege = teacherService.selectAvgCollege(teachers.getTeachercollege());
                            AvgCollege avgCollege1 = new AvgCollege();
                            if (avgCollege != null) {

                                String colleagueTwoIndexScore1="";
//                                String studentTwoindexscore1="";
                                String[] indexScores=indexScore.split(",");
                                for (int c=0;c<indexScores.length;c++) {
                                    String twoIndexName = indexScores[c].split("!")[1];//这次评价的二级指标名
                                    float twoScore = Float.parseFloat(indexScores[c].split("!")[2]);//二级指标得分
                                    if (avgCollege.getColltwoindexscore() != null) {

                                        String[] Twoindexscores = avgCollege.getColltwoindexscore().split("!!");//学校表中已经有的二级指标；双感叹号分割
//                                        String[] studentTwoindexscores = avgCollege.getStutwoindexscore().split("!!");//学生评价已有的二级指标；双感叹号分割

                                        List<String> list=new ArrayList<String>();
//                                        List<String> list1=new ArrayList<String>();

                                        //学院督导二级指标评价统计
                                        for (int a = 0; a < Twoindexscores.length; a++) {
                                            String[] Twoindexscore = Twoindexscores[a].split("_");
                                            String twoIndex = Twoindexscore[0];//已有的二级指标名
                                            float score = Float.parseFloat(Twoindexscore[1]);//总分数
                                            int number = Integer.parseInt(Twoindexscore[2]);//总人数

                                            list.add(twoIndex);

                                            if (twoIndex.equals(twoIndexName)) {
                                                score = score + twoScore;
                                                number = number + 1;

                                                if (colleagueTwoIndexScore1.equals("")) {
                                                    colleagueTwoIndexScore1 = twoIndex + "_" + score + "_" + number;
                                                } else {
                                                    colleagueTwoIndexScore1 = colleagueTwoIndexScore1 + "!!" + twoIndex + "_" + score + "_" + number;
                                                }
                                            }
//                                            else {
//                                                if (colleagueTwoIndexScore1.equals("")) {
//                                                    colleagueTwoIndexScore1 = twoIndex + "_" + score + "_" + number;
//                                                } else {
//                                                    colleagueTwoIndexScore1 = colleagueTwoIndexScore1 + "!!" + twoIndex + "_" + score + "_" + number;
//                                                }
//                                            }
                                        }
                                        if (!list.contains(twoIndexName)){
                                            if (colleagueTwoIndexScore1.equals("")) {
                                                colleagueTwoIndexScore1 = twoIndexName + "_" + twoScore + "_" + 1;
                                            } else {
                                                colleagueTwoIndexScore1 = colleagueTwoIndexScore1 + "!!" + twoIndexName + "_" + twoScore + "_" + 1;
                                            }
                                        }


                                    } else {
                                        colleagueTwoIndexScore1 = twoIndexName + "_" + twoScore + "_" + 1;

                                    }
                                }

                                avgCollege1.setSchoolcollegeid(avgCollege.getSchoolcollegeid());
                                avgCollege1.setColltwoindexscore(colleagueTwoIndexScore1);

                                int a = teacherService.updateAvgCollege(avgCollege1);
                                if (a <= 0) {
                                    map.put("code", 209);
                                    map.put("message", "修改学院平均分失败");
//                                    map.put("data", "");
                                    return map;
                                }
                            } else {
                                String[] indexScores=indexScore.split(",");
                                String twoIndexScoreN="";
                                for (int e=0;e<indexScores.length;e++){

                                    String twoIndexName = indexScores[e].split("!")[1];//二级指标名
                                    float twoScore = Float.parseFloat(indexScores[e].split("!")[2]);//二级指标得分

                                    if (twoIndexScoreN.equals("")){
                                        twoIndexScoreN=twoIndexName+"_"+twoScore+"_"+1;
                                    }else {
                                        twoIndexScoreN=twoIndexScoreN+"!!"+twoIndexName+"_"+twoScore+"_"+1;
                                    }
                                }

                                avgCollege1.setSchoolcollegeid(teachers.getTeachercollege());
                                avgCollege1.setColltwoindexscore(twoIndexScoreN);

                                int a = teacherService.insertAvgCollege(avgCollege1);
                                if (a <= 0) {
                                    map.put("code", 209);
                                    map.put("message", "添加学院平均分失败");
//                                    map.put("data", "");
                                    return map;
                                }
                            }

                        } else {
                            map.put("code", 210);
                            map.put("message", "学院查询失败");
//                            map.put("data", "");
                            return map;
                        }

                    }

                    break;
                }
                break;
            }
        }else {
            map.put("code", 211);
            map.put("message", "小组匹配失败");
//            map.put("data", "");
        }

        return map;
    }
    /**
     * 对老师进行自定义评价（无论评价人是谁）
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "CustomEvaluationColleagues")
    public synchronized @ResponseBody Map<String, Object> CustomEvaluationColleagues(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();

        String byEvaluatePersonId = request.getParameter("byEvaluatePersonId");//前端传入被评价人id
        String EvaluatePersonId = request.getParameter("appraiserId");//前端传入评价人id
        String role=request.getParameter("role");//前端传入一个评价人的角色，区分评价人是学生还是老师（student或teacher）

        String token=request.getParameter("token");

        //通过评价人id查出头像、昵称

        if (role.equals("teacher")) {//评价人是老师
            TeacherData teacher;
            teacher = (TeacherData) tokenController.token(EvaluatePersonId, token);
            if (teacher==null) {
                map.put("code", 202);
                map.put("message", "您暂无权限做此操作");
//                map.put("data", "");
                return map;
            }
//            TeacherData teacher = teacherService.selectByTeacherId(EvaluatePersonId);
            String mynickname = teacher.getMynickname();//昵称
            if (mynickname==null||mynickname.equals("")){
                mynickname=teacher.getTeachername();
            }

            String Teacherheadportrait = teacher.getTeacherheadportrait();//头像
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String evaluateTime = dateFormat.format(now);//时间
            String evaluationContent = request.getParameter("evaluationContent");//自定义评价内容

            if (evaluationContent==null||evaluationContent==""){
                map.put("code", 201);
                map.put("message", "评价内容不能为空");
//                map.put("data", "");
                return map;
            }

            CustomEvaluation customEvaluation = new CustomEvaluation(byEvaluatePersonId, EvaluatePersonId, mynickname, evaluateTime, Teacherheadportrait, evaluationContent);

            int b = evaluateTeacherService.insertcustomEvaluation(customEvaluation);

            if (b > 0) {
                //添加日志！！
                //操作内容
                TeacherStatistics teacherStatistics = teacherService.selectStatistics(byEvaluatePersonId);

                if (teacherStatistics==null){
                    TeacherStatistics teacherStatistics2=new TeacherStatistics();
                    teacherStatistics2.setTeacherid(byEvaluatePersonId);

                    teacherStatistics2.setBecomments(1);

                    teacherService.insertStatistics(teacherStatistics2);

                }else {
                    //System.out.println("进入else");
                    TeacherStatistics teacherStatistics1 = new TeacherStatistics();
                    teacherStatistics1.setTeacherid(byEvaluatePersonId);
//                                if (data.equals("beViewed")) {
                    int beComments = teacherStatistics.getBecomments();
                    int beComments1 = 1+beComments ;
                    teacherStatistics1.setBecomments(beComments1);
//                                    map1.put("beViewed", beViewed1);
                    //System.out.println("修改后："+beComments1);
                    teacherService.updateStatistics(teacherStatistics1);
//                                }
                }


                String logContent="老师自定义评价“"+byEvaluatePersonId+"”成功";
                //System.out.println(logContent);
                logController.logTeacher(logContent, EvaluatePersonId);

                map.put("code", 200);
                map.put("message", "评价成功");
//                map.put("data", "");
            } else {
                map.put("code", 400);
                map.put("message", "评价失败");
//                map.put("data", "");
            }
        }else if (role.equals("student")){//评价人是学生
            StudentApp studentApp;
            studentApp = (StudentApp) tokenController.token1(EvaluatePersonId, token);
            if (studentApp==null) {
                map.put("code", 202);
                map.put("message", "您暂无权限做此操作");
//                map.put("data", "");
                return map;
            }
//            StudentApp studentApp = studentService.selectData(EvaluatePersonId);
            String mynickname = studentApp.getMynickname();//昵称
            String Teacherheadportrait = studentApp.getStudentheadportrait();//头像
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String evaluateTime = dateFormat.format(now);//时间
            String evaluationContent = request.getParameter("evaluationContent");//自定义评价内容

            CustomEvaluation customEvaluation = new CustomEvaluation(byEvaluatePersonId, EvaluatePersonId, mynickname, evaluateTime, Teacherheadportrait, evaluationContent);

            int b = evaluateTeacherService.insertcustomEvaluation(customEvaluation);

            if (b > 0) {
                //添加日志！！
                //操作内容
                TeacherStatistics teacherStatistics = teacherService.selectStatistics(byEvaluatePersonId);

                if (teacherStatistics==null){
                    TeacherStatistics teacherStatistics2=new TeacherStatistics();
                    teacherStatistics2.setTeacherid(byEvaluatePersonId);
//                                if (data.equals("beComments")){
                    teacherStatistics2.setBecomments(1);
//                                    map1.put("beComments",1);
//                                }
                    teacherService.insertStatistics(teacherStatistics2);

                }else {
                    //System.out.println("进入else");
                    TeacherStatistics teacherStatistics1 = new TeacherStatistics();
                    teacherStatistics1.setTeacherid(byEvaluatePersonId);
//                                if (data.equals("beViewed")) {
                    int beComments = teacherStatistics.getBecomments();
                    int beComments1 = 1+beComments ;
                    teacherStatistics1.setBecomments(beComments1);
//                                    map1.put("beViewed", beViewed1);
                    //System.out.println("修改后："+beComments1);
                    teacherService.updateStatistics(teacherStatistics1);
//                                }
                }
                int commentNum=studentApp.getCommentnum()+1;
                StudentApp studentApp1=new StudentApp();
                studentApp1.setStudentid(EvaluatePersonId);
                studentApp1.setCommentnum(commentNum);

                studentAppService.updateByPrimaryKeySelective(studentApp1);

                String logContent="学生自定义评价“"+byEvaluatePersonId+"”成功";
                //System.out.println(logContent);
                logController.logStudent(logContent, EvaluatePersonId);

                map.put("code", 200);
                map.put("message", "评价成功");
//                map.put("data", "");
            } else {
                map.put("code", 400);
                map.put("message", "评价失败");
//                map.put("data", "");
            }
        }
    return map;
    }

}